package com.a3dweijing.wetouch2.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.a3dweijing.wetouch2.View.GlideRoundedCornersTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * Created by Administrator on 2018/3/19.
 */

public class GlideUtil
{
    private static final byte[] LOCKER = new byte[0];
    
    private static GlideUtil mInstance;
    
    private static String head = "http://wetouch.oss-cn-hangzhou.aliyuncs.com/";
    
    private static String head2 = "http://yiletech.oss-cn-shenzhen.aliyuncs.com/";
    
    // private static String head="http://video.3dweijing.com/";
    private static String foot = "?x-oss-process=style/img_lay";
    
    /**
     * @param url
     * @return url
     */
    public String checkUrl(String url)
    {
        if (url != null)
        {
            if (url.startsWith("http"))
            {
                return url;
            }
            else if (url.startsWith("wetouch"))
            {
                return head + url + foot;
            }
            else if (url.startsWith("upload"))
            {
                return head2 + url;
            }
            else
            {
                return "http://oss.3dweijing.com/upload/" + url + foot;
            }
        }
        return null;
    }
    
    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param view
     */
    public void showImage(Context context, String url, ImageView view)
    {
        Glide.with(context).load(url).into(view);
    }

    /**
     * 自动填满imageview 高度不固定，imageview需要设置  android:scaleType="fitXY"
     * @param context
     * @param url
     * @param view
     */
    public void showAutoImage(final Context context, String url, final ImageView view)
    {
        Glide.with(context).load(url).into(new SimpleTarget<Drawable>()
        {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition)
            {
                int imageWidth = resource.getMinimumWidth();
                int imageHeight = resource.getMinimumHeight();
                int height = getScreenWidth(context) * imageHeight / imageWidth;
                ViewGroup.LayoutParams para = view.getLayoutParams();
                para.height = height;
                para.width = getScreenWidth(context);
                view.setLayoutParams(para);
                view.setImageDrawable(resource);
            }
        });
    }
    
    public static int getScreenWidth(Context context)
    {
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
    
    /**
     * @param context
     * @param url
     * @param view
     */
    public void showByCircle(final Context context, String url, ImageView view)
    {
        Glide.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(view);
    }

    /**
     * 自定圆角
     * @param context
     * @param url
     * @param view
     * @param i
     * @param cornerType
     */
    public void showByCircle(final Context context, String url, ImageView view, int i,
        GlideRoundedCornersTransform.CornerType cornerType)
    {
        RequestOptions requestOptions = new RequestOptions();
        // requestOptions.circleCrop();
        // requestOptions.optionalTransform(new CircleCrop());
        // requestOptions.optionalTransform(new RoundedCorners(100));
        requestOptions.optionalTransform(new GlideRoundedCornersTransform(i, cornerType));
        Glide.with(context).load(url).apply(requestOptions).into(view);
    };
    
    private GlideUtil()
    {
    }
    
    public static GlideUtil getInstance()
    {
        if (mInstance == null)
        {
            synchronized (LOCKER)
            {
                if (mInstance == null)
                {
                    mInstance = new GlideUtil();
                }
            }
        }
        return mInstance;
    }
}
